package org.yoyo.classroom.service.impl;

import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.github.pagehelper.PageInfo;
import org.yoyo.classroom.domain.po.BookingOrder;
import org.yoyo.classroom.domain.vo.AvailableBookingTimeVO;
import org.yoyo.classroom.mapper.BookingOrderMapper;

import org.yoyo.classroom.mapper.StudyRoomMapper;
import org.yoyo.classroom.service.BookingOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 预约订单业务逻辑层实现类
 */
@Service
public class BookingOrderServiceImpl implements BookingOrderService {

    @Autowired
    private BookingOrderMapper bookingOrderMapper;
    @Autowired
    private StudyRoomMapper studyRoomMapper;

    /**
     * 根据ID查询预约订单
     * @param id 预约订单ID
     * @return 预约订单对象
     */
    @Override
    public BookingOrder getBookingOrderById(Long id) {
        return bookingOrderMapper.selectById(id);
    }

    /**
     * 查询所有预约订单
     * @return 预约订单列表
     */
    @Override
    public List<BookingOrder> getAllBookingOrders() {
        return bookingOrderMapper.selectAll();
    }

    /**
     * 插入预约订单
     * @param bookingOrder 预约订单对象
     * @return 影响的行数
     */
    @Override
    public int addBookingOrder(BookingOrder bookingOrder) {
        System.out.println(bookingOrder);
        return 0;


        //return bookingOrderMapper.insert(bookingOrder);
    }

    /**
     * 更新预约订单
     * @param bookingOrder 预约订单对象
     * @return 影响的行数
     */
    @Override
    public int updateBookingOrder(BookingOrder bookingOrder) {
        return bookingOrderMapper.update(bookingOrder);
    }

    /**
     * 删除预约订单（逻辑删除）
     * @param id 预约订单ID
     * @return 影响的行数
     */
    @Override
    public int deleteBookingOrder(Long id) {
        return bookingOrderMapper.delete(id);
    }

    @Override
    public List<BookingOrder> getBookingOrders() {
        return bookingOrderMapper.getBookingOrders();
    }

    @Override
    public List<BookingOrder> findOrderByUserId(Long userId) {
        return bookingOrderMapper.getOrdersByUserId(userId);
    }

    @Override
    public List<AvailableBookingTimeVO> getAvailableBookingTimes(Long roomId, Long seatId) {
        int maxHoursPerBooking = studyRoomMapper.getRoomRuleById(roomId).getMaxHoursPerBooking();
        List<AvailableBookingTimeVO> availableBookingTimes = studyRoomMapper.getAvailableBookingTimes(roomId, seatId);

        for (AvailableBookingTimeVO vo : availableBookingTimes) {
            Date timeSlot = vo.getTimeSlot();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(timeSlot);
            calendar.add(Calendar.HOUR_OF_DAY, maxHoursPerBooking);
            vo.setEndTime(calendar.getTime());
        }

        return availableBookingTimes;
    }


    @Override
    public PageInfo<BookingOrder> findOrderByUserIdWithPage(Long userId, Integer pageNum, Integer pageSize, String bookingDate, Integer status) {
        // 使用 PageHelper 进行分页
        PageHelper.startPage(pageNum, pageSize);
        // 构建查询条件
        BookingOrder query = new BookingOrder();
        query.setUserId(userId);
        if (bookingDate != null) {
            //query.setBookingDate(bookingDate);
        }
        if (status != null) {
            query.setStatus(status);
        }
        // 执行查询
        List<BookingOrder> list = bookingOrderMapper.selectByCondition(userId,bookingDate,status);
        // 返回分页信息
        return new PageInfo<>(list);
    }
}
